/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.Company;
import org.crystalrange.service.CompanyService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class CompanyListController extends BaseController {

    protected CompanyService companyService;
    protected Company selectedCompany;
    protected Listbox listboxCompany;
    protected Search searchCompany;
    protected List<Company> companies;

    public CompanyService getCompanyService() {
        return this.companyService;
    }

    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    public Company getSelectedCompany() {
        return this.selectedCompany;
    }

    public void setSelectedCompany(Company selectedCompany) {
        this.selectedCompany = selectedCompany;
    }

    public List<Company> getCompanies() {
        return this.companies;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchCompany = new Search(Company.class);
        this.searchCompany.setMaxResults(100);
        this.searchCompany.addFetch("createdBy");
        this.searchCompany.addFetch("updatedBy");
        this.companies = this.companyService.search(this.searchCompany);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemCompanyCreate(Event event) throws Exception {
        this.companyCreate();
    }

    public void onClick$menuitemCompanyEdit(Event event) throws Exception {
        this.companyEdit();
    }

    public void onClick$menuitemCompanyDelete(Event event) throws Exception {
        this.companyDelete();
    }

    public void onClick$menuitemCompanySearch(Event event) throws Exception {
        this.companySearch();
    }

    public void onClick$menuitemCompanyRefresh(Event event) throws Exception {
        this.companyRefresh();
    }

    public void onClick$toolbarbuttonCompanyCreate(Event event) throws Exception {
        this.companyCreate();
    }

    public void onClick$toolbarbuttonCompanyEdit(Event event) throws Exception {
        this.companyEdit();
    }

    public void onClick$toolbarbuttonCompanyDelete(Event event) throws Exception {
        this.companyDelete();
    }

    public void onClick$toolbarbuttonCompanySearch(Event event) throws Exception {
        this.companySearch();
    }

    public void onClick$toolbarbuttonCompanyRefresh(Event event) throws Exception {
        this.companyRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.companyEdit();
    }
    
    private void companyCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/company.zul");
    }

    private void companyEdit() throws Exception {
        if (this.selectedCompany != null) {
            this.execution.sendRedirect("~./zul/html/company.zul?Id=" + this.selectedCompany.getId().toString());
        }
    }

    private void companyDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxCompany.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Company company = (Company) selectedItem.getValue();
                    this.companyService.remove(company);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.companies = this.companyService.search(this.searchCompany);
        this.binder.loadAll();
    }

    private void companySearch() throws Exception {
        this.execution.setAttribute("searchCompany", this.searchCompany);

        Window window = (Window) this.execution.createComponents("~./zul/html/companySearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchCompany = (Search) this.execution.getAttribute("searchCompany");
        }

        this.companies = this.companyService.search(this.searchCompany);
        this.binder.loadAll();
    }

    private void companyRefresh() throws Exception {
        this.companies = this.companyService.search(this.searchCompany);
        this.binder.loadAll();
    }
}
